package com.ruoyi.web.controller.market.sys;

import java.time.LocalDate;
import java.util.*;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.db.model.param.ProfitInfoParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.AppProfitInfo;
import com.ruoyi.profit.service.IAppProfitInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.wapper.AppProfitInfoWapper;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目收益Controller
 * 
 * @author ruoyi
 * @date 2022-12-22
 */
@Controller
@RequestMapping("/profit/profit")
public class AppProfitInfoController extends BaseController
{
    private String prefix = "profit/profit";

    @Autowired
    private IAppProfitInfoService appProfitInfoService;

    @RequiresPermissions("profit:profit:view")
    @GetMapping()
    public String profit()
    {
        return prefix + "/profit";
    }

    @RequiresPermissions("profit:profit:view")
    @GetMapping("/chart")
    public String chart()
    {
        return prefix + "/chart";
    }

    /**
     * 查询项目收益列表
     */
    @RequiresPermissions("profit:profit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProfitInfoParam profitInfoParam)
    {
        startPage();
        List<Map<String, Object>> mapList = appProfitInfoService.selectAppProfitInfoListMap(profitInfoParam);

        // 查询总数据
        Map<String, Object> totalMap = appProfitInfoService.selectAppProfitInfoTotalMap(profitInfoParam);
        if (ObjectUtil.isNotEmpty(totalMap)){
            mapList.add(0, totalMap);
        }

        mapList = new AppProfitInfoWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 查询收益列表数据
     *
     */
    @RequiresPermissions("profit:profit:list")
    @PostMapping("/chartList")
    @ResponseBody
    public AjaxResult chartList(ProfitInfoParam profitInfoParam)
    {

        if (StrUtil.isEmpty(profitInfoParam.getDateInfo())){
            profitInfoParam.setStartDateInfo(LocalDate.now().minusDays(15).toString());
            profitInfoParam.setEndDateInfo(LocalDate.now().toString());
        }

        List<AppProfitInfo> dataList = appProfitInfoService.selectAppProfitInfoList(profitInfoParam);

        List<String> titleInfoList = Arrays.asList("总收益", "助贷收益", "贷超收益");

        List<String> dateInfoList = new ArrayList<>();

        List<String> totalDataList = new ArrayList<>();
        List<String> zdDataList = new ArrayList<>();
        List<String> dcDataList = new ArrayList<>();

        dataList.stream().forEach(appProfitInfo -> {

            dateInfoList.add(appProfitInfo.getDateInfo());

            totalDataList.add(String.valueOf(appProfitInfo.getTotalProfit()));
            zdDataList.add(String.valueOf(appProfitInfo.getZdProfit()));
            dcDataList.add(String.valueOf(appProfitInfo.getDcProfit()));

        });

        List<Map<String, Object>> chartDataList = new ArrayList<>();
        Map<String, Object> totalMapList = new HashMap<>();
        totalMapList.put("name", "总收益");
        totalMapList.put("type", "line");
        totalMapList.put("data", totalDataList);

        Map<String, Object> zdMapList = new HashMap<>();
        zdMapList.put("name", "助贷收益");
        zdMapList.put("type", "line");
        zdMapList.put("data", zdDataList);

        Map<String, Object> dcMapList = new HashMap<>();
        dcMapList.put("name", "贷超收益");
        dcMapList.put("type", "line");
        dcMapList.put("data", dcDataList);

        chartDataList.add(totalMapList);
        chartDataList.add(zdMapList);
        chartDataList.add(dcMapList);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dateInfoList", dateInfoList);
        jsonObject.put("titleInfoList", titleInfoList);
        jsonObject.put("chartDataList", chartDataList);

        return AjaxResult.success(jsonObject);
    }

    /**
     * 导出项目收益列表
     */
    @RequiresPermissions("profit:profit:export")
    @Log(title = "项目收益", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppProfitInfo appProfitInfo)
    {
        List<AppProfitInfo> list = null;//appProfitInfoService.selectAppProfitInfoList(appProfitInfo);
        ExcelUtil<AppProfitInfo> util = new ExcelUtil<AppProfitInfo>(AppProfitInfo.class);
        return util.exportExcel(list, "项目收益数据");
    }

    /**
     * 新增项目收益
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目收益
     */
    @RequiresPermissions("profit:profit:add")
    @Log(title = "项目收益", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppProfitInfo appProfitInfo)
    {
        return toAjax(appProfitInfoService.insertAppProfitInfo(appProfitInfo));
    }

    /**
     * 修改项目收益
     */
    @RequiresPermissions("profit:profit:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AppProfitInfo appProfitInfo = appProfitInfoService.selectAppProfitInfoById(id);
        mmap.put("appProfitInfo", appProfitInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目收益
     */
    @RequiresPermissions("profit:profit:edit")
    @Log(title = "项目收益", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppProfitInfo appProfitInfo)
    {
        return toAjax(appProfitInfoService.updateAppProfitInfo(appProfitInfo));
    }

    /**
     * 删除项目收益
     */
    @RequiresPermissions("profit:profit:remove")
    @Log(title = "项目收益", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(appProfitInfoService.deleteAppProfitInfoByIds(ids));
    }
}
